<?php
/*
 * @Descripttion: 后台用户管理相关控制器
 * @version:
 * @Author: ewgof
 * @Date: 2019-11-12 16:27:09
 * @LastEditors: ewgof
 * @LastEditTime: 2020-10-08 07:03:30
 */
namespace app\http\controller\admin;

class Users extends Base
{
    /**
     * @name: index
     * @msg: 后台用户列表,以下参数均为可选参数
     * @param $page
     * @param $limit
     * @param $username
     * @return:
     */
    public function index($request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 15);
        $num = ($page - 1) * $limit;
        $keywords = $request->get('keywords');
        $status = $request->get('status');

        $where = [];
        if (!empty($keywords)) {
            $where['OR'] = [
                'username[~]' => $keywords,
                'phone[~]'  => $keywords,
                'email[~]' => $keywords,
            ];
        }
        if (!empty($status)) {
            $where['status'] = $status;
        }

        $where['ORDER'] = ['created_at' => 'DESC', 'updated_at' => 'DESC'];
        $where['LIMIT'] = [$num, $num + $limit];

        $all = $this->db->count('users', $where);
        $list = $this->db->select('users', ['id', 'username', 'status', 'spaces', 'phone', 'email', 'created_at', 'updated_at'], $where);
        foreach ($list as $lk => $lv) {
            $list[$lk]['spaces'] = humansize($lv['spaces'], true);
            $list[$lk]['used_spaces'] = \humansize($this->db->get('user_spaces', 'spaces', ['uid' => $lv['id']]), true);
        }
        $info = array(
            'all' => $all,
            'list' => $list,
        );
        return api_json_return(0, '', $info);
    }


    /**
     * @name: lock
     * @msg: 后台锁定用户账号，锁定后该用户账号不能登录，如果该用户正在登录，锁定后清除其登录状态，强制下线
     * @param $id integer 用户id
     * @param $type integer 操作类别，2：锁定，1：解锁
     * @return:
     */
    public function lock($request)
    {
        $input = $request->post();
        $id = $input['id'];
        $type = $input['type'];
        $status = $input['status'];

        if (empty($id) || empty($type) || ($type != 1 && $type != 2)) {
            return api_json_return(2);
        }
        $up_data = [
            'status' => $status,
            'updated_at' => $this->t,
        ];

        if ($type == 1) {
            $up_data['token'] = '';
            $up_data['expire'] = 0;
        }
        $info = $this->db->update('users', $up_data, ['id' => $id])->rowCount();
        // var_dump($this->db->last());
        if ($info === 1) {
            return api_json_return(0);
        } else {
            $msg = '锁定用户失败。' . 'info => ' . $info;
            echo $msg . PHP_EOL;
            log_msg($msg, 'error', $this->log_filename_apperr);
            return api_json_return(1);
        }
    }



    public function resetSpaces($request)
    {
        $id = $request->post('id');
        $spaces = $request->post('spaces');
        if (empty($id) || empty($spaces)) {
            return api_json_return(2);
        }
        if (!\is_numeric($spaces)) {
            return api_json_return(2);
        }

        $info = $this->db->update('users', ['spaces' => $spaces, 'updated_at' => $this->t], ['id' => $id])->rowCount();
        if ($info === 1) {
            return api_json_return(0);
        } else {
            $msg = '重置用户可用空间大小失败。id => ' . $id . ', spaces => ' . $spaces . ', info => ' . $info;
            echo $msg . PHP_EOL;
            log_msg($msg, 'error', $this->log_filename_apperr);
            return api_json_return(1);
        }
    }


    public function delete($request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return api_json_return(2);
        }
        $this->db->pdo->beginTransaction();
        $info_u = $this->db->delete('users', ['id' => $id])->rowCount();
        $info_u_s = $this->db->delete('user_spaces', ['uid' => $id])->rowCount();
        if ($info && $info_u) {
            $this->db->pdo->commit();
            return api_json_return(0);
        } else {
            $this->db->pdo->rollback();
            $msg = '删除用户失败。id => ' . $id . ', info_u => ' . $info_u . ', info_u_s => ' . $info_u_s;
            echo $msg . PHP_EOL;
            log_msg($msg, 'error', $this->log_filename_apperr);
            return api_json_return(1);
        }
    }
}
